package test;

import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;
import utils.ESClientUtil;

import java.util.HashMap;

public class ESTest2 {
    @Test
    public void testAdd(){
        // 1.获取es连接
        TransportClient client = ESClientUtil.getClient();
        // 2.准备放入es中的数据
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", 1);
        map.put("name", "中国人");
        map.put("age", 21);
        map.put("sex", "1");
        // 3.使用链式编程将数据保存到es中
        IndexRequestBuilder builder = client.prepareIndex("pethome", "pet", "1").setSource(map);
        // 4.得到 添加文档结果
        System.out.println(builder.get());
        // 5.关闭连接
        client.close();
    }

    @Test
    public void testGet(){
        // 1.获取es连接
        TransportClient client = ESClientUtil.getClient();
        // 3.使用链式编程将数据保存到es中
        GetResponse documentFields = client.prepareGet("pethome", "pet", "1").get();
        // 4.得到 添加文档结果
        System.out.println(documentFields);
        System.out.println(documentFields.getSource());
        // 5.关闭连接
        client.close();
    }

    @Test
    public void testUpdate(){
        // 1.获取es连接
        TransportClient client = ESClientUtil.getClient();
        // 2.准备修改的数据
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", "美国人");
        // 3.使用链式编程将数据保存到es中
        UpdateResponse updateResponse = client.prepareUpdate("pethome", "pet", "1").setDoc(map).get();
        // 4.得到 添加文档结果
        System.out.println(updateResponse);
        // 5.关闭连接
        client.close();
    }

    @Test
    public void testDel(){
        // 1.获取es连接
        TransportClient client = ESClientUtil.getClient();
        // 2.准备修改的数据
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", "美国人");
        // 3.使用链式编程将数据保存到es中
        DeleteResponse deleteResponse = client.prepareDelete("pethome", "pet", "1").get();
        // 4.得到 添加文档结果
        System.out.println(deleteResponse);
        // 5.关闭连接
        client.close();
    }

    @Test
    public void testAdds(){
        // 1.获取es连接
        TransportClient client = ESClientUtil.getClient();
        for(int i = 2 ; i<100; i++){
            IndexRequestBuilder indexRequestBuilder = client.prepareIndex("pethome", "employee");
            indexRequestBuilder.setId(i+"");
            HashMap<String, Object> map = new HashMap<>();
            map.put("id", i);
            if (i%3 == 0) {
                if (i % 3==0){
                    map.put("name", "我在源码--" + i);
                }else if(i % 3==1){
                    map.put("name", "我在天上人间--" + i);
                }else {
                    map.put("name", "我在害怕--" + i);
                }
            }
            map.put("age", i);
            map.put("sex", i%2==1);
            // 3.放入新增数据，同时获取到执行结果
            IndexResponse indexResponse = indexRequestBuilder.setSource(map).get();
            System.out.println(indexResponse);
        }
        client.close();
    }

    @Test
    public void testComplex() throws Exception{
        TransportClient client = ESClientUtil.getClient();
        SearchRequestBuilder requestBuilder = client.prepareSearch("pethome");
        SearchRequestBuilder employee = requestBuilder.setTypes("employee");
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.matchQuery("name","我在源码"))
                .filter(QueryBuilders.termQuery("sex",true))
                .filter(QueryBuilders.rangeQuery("age").gte(1).lte(12));
        employee.setQuery(boolQueryBuilder);
        employee.setSize(10);
        employee.setFrom(0);
        employee.addSort("age", SortOrder.DESC);
        SearchResponse searchResponse = employee.get();
        SearchHit[] hits = searchResponse.getHits().getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsMap());
        }
    }
}
